package com.example.chatservice.webSocket;



import lombok.extern.slf4j.Slf4j;
import org.example.entity.dto.MessageSendDto;
import org.example.utils.JsonUtils;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @Description
 * @Author susu
 * @Description 消息处理器
 * @Date 2024/9/20
 */
@Component
@Slf4j
public class MessageHandler {

    private static final String MESSAGE_TOPIC = "message.topic";
    @Resource
    private RedissonClient redissonClient;

    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private ChannelContextUtils channelContextUtils;

    //成员变量注入完之后才会执行 后置处理器的前置
    @PostConstruct
    public void listenMessage(){
        RTopic rTopic = redissonClient.getTopic(MESSAGE_TOPIC);

        rTopic.addListener(MessageSendDto.class,(MessageSendDto, sendDto)->{
            log.info("收到广播消息{}", JsonUtils.convertObj2Json(sendDto));
            channelContextUtils.sendMessage(sendDto);
        });
    }
    public void sendMessage(MessageSendDto messageSendDto){
        RTopic rTopic = redissonClient.getTopic(MESSAGE_TOPIC);
        rTopic.publish(messageSendDto);

    }
}
